<template>
  <div class="wrap-item">
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model.trim="formSearch.param.navName"
          placeholder="请输入导航名称"
          maxlength="8"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.navType"
          placeholder="请选择导航类型"
        >
          <el-option label="头部导航" value="1"></el-option>
          <el-option label="底部导航" value="2"></el-option>
          <el-option label="友情链接" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
        <span
          class="wrap-btn wrap-success"
          v-if="$checkPermission(101)"
          @click="add"
          >添加导航</span
        >
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-item-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'child', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="navName" label="导航名称"></el-table-column>
        <el-table-column prop="navType" label="导航类型">
          <template slot-scope="scope">
            <span v-if="scope.row.navType == 1">头部导航</span>
            <span v-if="scope.row.navType == 2">底部导航</span>
            <span v-if="scope.row.navType == 3">友情链接</span>
          </template>
        </el-table-column>
        <el-table-column prop="addName" label="创建人"></el-table-column>
        <el-table-column
          prop="addTime"
          label="创建时间"
          width="160px"
        ></el-table-column>
        <el-table-column
          prop="sort"
          label="排序"
          width="80px"
          align="center"
        ></el-table-column>
        <el-table-column label="状态" width="80px" align="center">
          <template slot-scope="scope">
            <el-link
              type="success"
              :underline="false"
              v-if="scope.row.status == 0"
              >启用</el-link
            >
            <el-link
              type="danger"
              :underline="false"
              v-if="scope.row.status == 1"
              >停用</el-link
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item command="edit" v-if="$checkPermission(102)"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.status == 1 && $checkPermission(105)"
                  >启用</el-dropdown-item
                >
                <el-dropdown-item
                  command="changeStatus"
                  v-if="scope.row.status == 0 && $checkPermission(106)"
                  >停用</el-dropdown-item
                >
                <el-dropdown-item command="remove" v-if="$checkPermission(103)"
                  >删除</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 对话框 -->
    <el-drawer :title="title" :wrapperClosable="false" :visible.sync="visible">
      <el-form label-width="80px" :model="form" size="medium">
        <el-form-item label="导航名称" required>
          <el-input
            v-model.trim="form.navName"
            clearable
            maxlength="8"
            placeholder="请输入导航名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="导航类型" required>
          <el-select
            clearable
            v-model="form.navType"
            placeholder="请选择导航类型"
            @change="navList"
            style="width: 100%"
          >
            <el-option label="头部导航" value="1"></el-option>
            <el-option label="底部导航" value="2"></el-option>
            <el-option label="友情链接" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="父级导航" v-if="form.navType == 2">
          <el-select
            clearable
            v-model="form.parentNo"
            placeholder="请选择父级导航"
            style="width: 100%"
          >
            <el-option
              v-for="(item, index) in nav"
              :key="index"
              :label="item.navName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="登录" required>
          <el-select
            v-model="form.loginFlag"
            placeholder="请选择是否需要登录"
            style="width: 100%"
          >
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" required>
          <el-input-number
            v-model="form.sort"
            :min="1"
            :max="50"
            :step="1"
            step-strictly
            style="width: 100%"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="导航链接">
          <el-input
            v-model.trim="form.url"
            clearable
            maxlength="100"
            placeholder="请输入导航链接"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="save">确定</span>
          <span class="wrap-btn wrap-default" @click="visible = false"
            >取消</span
          >
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 查看 -->
    <el-dialog title="查看" :visible.sync="isLook" width="888px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="导航名称" :span="2">
          {{ lockData.navName }}
        </el-descriptions-item>
        <el-descriptions-item label="链接" :span="2">
          {{ lockData.url }}
        </el-descriptions-item>
        <el-descriptions-item label="导航类型">
          <span v-if="lockData.navType == 1">头部导航</span>
          <span v-if="lockData.navType == 2">底部导航</span>
          <span v-if="lockData.navType == 3">友情链接</span>
        </el-descriptions-item>
        <el-descriptions-item label="状态">
          {{ lockData.status == 0 ? '启用' : '停用' }}
        </el-descriptions-item>
        <el-descriptions-item label="创建人">
          {{ lockData.addName }}
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">
          {{ lockData.addTime }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      nav: [],
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      total: null,
      tableData: [],
      visible: false,
      title: '添加导航',
      form: {},
      isLook: false,
      lockData: {},
    }
  },
  created() {
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios
        .post('/admin/portal/nav/page', this.formSearch)
        .then((data) => {
          this.tableData = []
          if (data.code == 200) {
            this.tableData = data.data.list
            this.total = data.data.total
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    add() {
      //添加
      this.title = '添加导航'
      this.form = { status: '1',loginFlag:'0' }
      this.visible = true
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          //查看
          this.$axios
            .get('/admin/portal/nav/find?id=' + row.id)
            .then((data) => {
              this.lockData = {}
              if (data.code == 200) {
                this.lockData = data.data
                this.isLook = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'edit':
          this.title = '编辑导航'
          this.$axios
            .get('/admin/portal/nav/find?id=' + row.id)
            .then((data) => {
              this.form = { status: '1' ,loginFlag:'0' }
              if (data.code == 200) {
                this.form = data.data
                this.form.navType = String(data.data.navType)
                this.form.loginFlag = String(data.data.loginFlag)
                this.visible = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'changeStatus':
          //启用停用
          const status = row.status == 0 ? 1 : 0
          this.$axios
            .get(`/admin/portal/nav/updateStatus?id=${row.id}&status=${status}`)
            .then((data) => {
              if (data.code == 200) {
                this.itemList()
                this.$message.success('操作成功！')
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'remove':
          //删除
          this.$confirm('您确定要删除该条数据吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              this.$axios
                .get('/admin/portal/nav/del?id=' + row.id)
                .then((data) => {
                  if (data.code == 200) {
                    this.itemList()
                    this.$message.success('操作成功！')
                  } else {
                    this.$message.error(data.msg)
                  }
                })
            })
            .catch(() => {})
          break
      }
    },
    save() {
      //保存
      if (!this.form.navName) {
        this.$message.error('请输入导航名称')
        return
      }
      if (!this.form.navType) {
        this.$message.error('请选择导航类型')
        return
      }
      if (!this.form.parentNo && this.form.navType == 2) {
        this.$message.error('请选择父级导航')
        return
      }
      if (!this.form.loginFlag) {
        this.$message.error('请选择是否需要登录')
        return
      }
      if (!this.form.sort) {
        this.$message.error('请输入排序')
        return
      }
      this.$axios.post('/admin/portal/nav/add', this.form).then((data) => {
        if (data.code == 200) {
          this.visible = false
          this.$message.success('操作成功！')
          this.itemList()
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    navList(val) {
      // 获取列表
      this.$axios.get(`/admin/portal/nav/list?navType=${val}`).then((data) => {
        this.nav = []
        if (data.code == 200) {
          this.nav = data.data
        } else {
          this.$message.error(data.msg)
        }
      })
    },
  },
}
</script>
